namespace MapleRock.P2PEmulator.Test.Profiling
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Text;

  using Core;
  using Core.Events;

  using NUnit.Framework;

  [TestFixture]
  public sealed class RoutingProfiling
  {
    [TestCase]
    public void ProfileSearchRequest()
    {
      const int PeersCount = 300;

      var network = Utils.CreateNetwork(2, 30, 30, 4, 1, 3, PeersCount, PeerBehaviour.DoesNothing);

      network.PrepareRun();

      // let the nodes to self-organize themselfes in the proper 2D mesh 
      network.Go(5);

      // search for the peer which is in the left-bottom corner of the 2D i-space
      var fromPeer = (Peer)null;
      var minX = double.MaxValue;
      var minY = double.MaxValue;
      foreach (var entry in network.State.Peers.Values)
      {
        if ((entry.AbsolutePosition[0] < minX) && (entry.AbsolutePosition[1] < minY))
        {
          minX = entry.AbsolutePosition[0];
          minY = entry.AbsolutePosition[1];
          fromPeer = entry;
        }
      }

      // search for the peer which is in the right-top corner of the 2D i-space
      var toPeer = (Peer)null;
      var maxX = double.MinValue;
      var maxY = double.MinValue;
      foreach (var entry in network.State.Peers.Values)
      {
        if ((entry.AbsolutePosition[0] > maxX) && (entry.AbsolutePosition[1] > maxY))
        {
          maxX = entry.AbsolutePosition[0];
          maxY = entry.AbsolutePosition[1];
          toPeer = entry;
        }
      }

      // create search request for the second peer
      var request = new MessageSearchRequest(
        network.State.GenerateUniqueId(),
        fromPeer.Address,
        Guid.NewGuid());
      request.Destination = (double[])toPeer.AbsolutePosition.Clone();
      request.From = fromPeer.Id;
      request.SearchRadius = 1;
      request.To = fromPeer.Id;

      // pass request to the incoming queue of the <from> peer
      fromPeer.Incoming.Enqueue(request);

      // and let the network to do its routing job
      network.Go(50);
    }
  }
}
